var ds,grid;

var buildGrid = function(){
    ds = new Ext.data.Store({
        proxy:new Ext.data.HttpProxy({url:'setting!list.action'}),
        reader:new Ext.data.JsonReader({idProperty: "setting.id"},['setting.id','setting.name','setting.position',{name:'setting.note'},'setting.code'])
    });
    
    grid = new Ext.grid.EditorGridPanel({
        store:ds,
        
        columns:[
            new Ext.grid.RowNumberer(),
            {header:'名称',width:150,sortable:true,dataIndex:'setting.name',editor:new Ext.form.TextField()},
            {header:'编码',width:250,sortable:true,dataIndex:'setting.code',editor:new Ext.form.TextField()},
            {header:'值',width:250,sortable:true,dataIndex:'setting.value',editor:new Ext.form.TextField()},
            {header:'排序',width:100,sortable:true,dataIndex:'setting.position',editor:new Ext.form.NumberField()},
            {header:'备注',width:250,sortable:true,dataIndex:'setting.note',editor:new Ext.form.TextField()}
        ],
        renderTo:'grid',
        border:true,
        clicksToEdit:2,
        selModel:new Ext.grid.RowSelectionModel()
    });
    
    ds.load();
};

var buildLayout = function(){
    new Ext.Viewport({
        layout:'border',
        defaults:{border:false},
        items:[{
            region:'north',
            contentEl:'north-div',
            title:'系统设置',
            tbar:[{
                text:'新增',
                iconCls:'admin-tool-add',
                handler:createSetting
            },'-',{
                text:'保存',
                iconCls:'admin-tool-save',
                handler:saveSetting
            },'-',{
                text:'删除',
               iconCls:'admin-tool-delete',
                handler:destroySetting
            },'->','双击表格可以修改字典资料']
        },{
            region:'center',
            layout:'fit',
            items:grid
        }]
    });
};

var init = function(){
    buildGrid();
    
  	buildLayout();
};

Ext.onReady(init);

var createSetting = function(){
    Ext.Ajax.request({
        url:'setting!create.action',
        params:{
            'setting.name':'新配置'
        },
        success:function(response){
            var data = Ext.util.JSON.decode(response.responseText);
            alert(data.success);
            if (data.success){
                ds.reload();
            }
            else{
                Ext.MessageBox.alert('警告',data.msg);
            }
        }
	})
};

var saveSetting = function(){
	var records = ds.getModifiedRecords();
	for (var i = 0; i < records.length; i++) {
		var params = records[i].data;
    	
		
        Ext.Ajax.request({
            url:'setting!update.action',
            params:params,
            method:'POST',
            success:function(response){
                var data = Ext.util.JSON.decode(response.responseText);
                var r = ds.getById(data.id);
				r.commit();
            }
        });
	}
};
var destroySetting = function(){

	var rows = grid.getSelectionModel().getSelections();
	
	if (rows.length == 0 ){
		Ext.Msg.alert("提示", "您还没有选择任何数据！");
		return;
	}
	Ext.MessageBox.confirm("删除确认", "是否真的删除选定的资料?", function(btn){
		if(btn=='yes'){
			var ids = [];
			if (rows.length > 0){
				for (var i = 0; i < rows.length; i++) {
					ids.push(rows[i].data['setting.id']);
				}
			}
			
			
		
			Ext.Ajax.request({
				url:'setting!delete.action',
				method:'POST',
				params:{'ids':ids},
				success:function(response){
					var data = Ext.util.JSON.decode(response.responseText);
					var rows = grid.getSelectionModel().getSelections();
					if (data.success){           
				        ds.reload();
				        Ext.MessageBox.alert('警告','删除区划资料完成。');
			        }
					else{
			            Ext.MessageBox.alert('警告',data.msg);
					}
				}
			})
		    	
		    
		    var rec = grid.getSelectionModel().getSelected();
		    if (rec){
		        id = rec.data['setting.id'];
		    }
		}
	});
   

}
